//
//  PongField.mm
//
//  Created by Jonathan Saggau on 2007-08-09.
//  Copyright (c) 2007 __MyCompanyName__. All rights reserved.
//

#import "PongField.h"
#import "Common.h"

@implementation PongField

-(id)initWithFieldRect:(NSRect)aFieldRect;
{
	self = [super init];
	if (nil != self)
	{
		fieldRect = aFieldRect;
		leftFinger = NSMakePoint(0.0, 0.0);
		rightFinger = NSMakePoint(0.0, 0.0);
		leftPaddle.size = NSMakeSize(50.0, 10.0);
		leftPaddle.origin.x = ((fieldRect.size.width * 0.5) - (leftPaddle.size.width *.5));
		leftPaddle.origin.y = 0.0;
		
		rightPaddle.size = leftPaddle.size;
		rightPaddle.origin.x = leftPaddle.origin.x;
		rightPaddle.origin.y = fieldRect.size.height - 10.0;
		ballRect.size = NSMakeSize(10.0, 10.0);
		ballRect.origin.x = (fieldRect.size.height * 0.5) - (ballRect.size.height * 0.5);
		ballRect.origin.y = (fieldRect.size.width * 0.5) - (ballRect.size.width * 0.5);
		
		leftScore = 0;
		rightScore = 0;
	}
	return self;
}

- (id)init
{	
    NSRect rect = NSMakeRect(0.0,0.0,320.0,410.0);
	return [self initWithFieldRect:rect];
}

-(void)dealloc
{
	[super dealloc];
}

-(void)drawInRect:(NSRect)Rect
{
    NSColor *everythingIsThisColor = [NSColor greenColor];
    [everythingIsThisColor set];
    
    NSRectFill(leftPaddle);
    NSRectFill(rightPaddle);
    NSRectFill(ballRect);
    
    NSFont *font = [NSFont fontWithName:@"Marker Felt" size:24.0];
    NSMutableDictionary *standardStrAttrib=[NSMutableDictionary dictionary];
    [standardStrAttrib setObject:font forKey:NSFontAttributeName];
    [standardStrAttrib setObject:everythingIsThisColor forKey:NSForegroundColorAttributeName];

    NSMutableAttributedString *score = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%i", [self rightScore]] attributes:standardStrAttrib];

    NSRect scoreRect = [self fieldRect];
    scoreRect.size = [score size];    
    scoreRect.origin.x += 10.0;
    scoreRect.origin.y += 10.0;
    [score drawInRect:scoreRect];
    
    [score replaceCharactersInRange:NSMakeRange(0,[score length]) withString:[NSString stringWithFormat:@"%i", [self leftScore]]];
    scoreRect.size = [score size];
    scoreRect.origin.y = [self fieldRect].size.height - scoreRect.size.height - 10.0;
    [score drawInRect:scoreRect];

    [score release];
}


//accessors
//=========================================================== 
//  fieldRect 
//=========================================================== 
- (NSRect)fieldRect
{
    return fieldRect;
}
- (void)setFieldRect:(NSRect)aFieldRect
{
    fieldRect = aFieldRect;
}

//=========================================================== 
//  leftFinger 
//=========================================================== 
- (NSPoint)leftFinger
{
    return leftFinger;
}
- (void)setLeftFinger:(NSPoint)aLeftFinger
{
    leftFinger = aLeftFinger;
}

//=========================================================== 
//  rightFinger 
//=========================================================== 
- (NSPoint)rightFinger
{
    return rightFinger;
}
- (void)setRightFinger:(NSPoint)aRightFinger
{
    rightFinger = aRightFinger;
}

//=========================================================== 
//  leftPaddle 
//=========================================================== 
- (NSRect)leftPaddle
{
    return leftPaddle;
}
- (void)setLeftPaddle:(NSRect)aLeftPaddle
{
    leftPaddle = aLeftPaddle;
}

//=========================================================== 
//  rightPaddle 
//=========================================================== 
- (NSRect)rightPaddle
{
    return rightPaddle;
}
- (void)setRightPaddle:(NSRect)aRightPaddle
{
    rightPaddle = aRightPaddle;
}

//=========================================================== 
//  leftScore 
//=========================================================== 
- (int)leftScore
{
    return leftScore;
}
- (void)setLeftScore:(int)aLeftScore
{
    NSLog(@"New Left Score = %i", aLeftScore);
    leftScore = aLeftScore;
}

//=========================================================== 
//  rightScore 
//=========================================================== 
- (int)rightScore
{
    return rightScore;
}
- (void)setRightScore:(int)aRightScore
{
    NSLog(@"New Right Score = %i", aRightScore);
    rightScore = aRightScore;
}

//=========================================================== 
//  ballRect 
//=========================================================== 
- (NSRect)ballRect
{
    return ballRect;
}
- (void)setBallRect:(NSRect)aBallRect
{
    ballRect = aBallRect;
}

@end
